import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';

import '../../api/interface/home_api.dart';
import '../../api/request/response_data.dart';
import '../../config/const.dart';
import '../../moudle/common_info.dart';

class DeviceControlInput extends StatefulWidget {
  final Map item;
  final Function callBack;
  const DeviceControlInput({super.key, required this.item, required this.callBack});

  @override
  State<DeviceControlInput> createState() => _DeviceControlInputState();
}

class _DeviceControlInputState extends State<DeviceControlInput> {

  TextEditingController controller = TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    controller.text = widget.item['value'] ?? '';
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 690.w,
      padding: EdgeInsets.symmetric(horizontal: 30.w,vertical: 60.w),
      decoration: BoxDecoration(
          color: Color(0xff1E1E1E),
          borderRadius: BorderRadius.circular(20.w)
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            decoration: BoxDecoration(
                border: Border.all(color: Color(0xff999999),width: 1),
                borderRadius: BorderRadius.circular(100.w)
            ),
            height: 100.w,
            padding: EdgeInsets.symmetric(horizontal: 30.w),
            child: TextField(
              //控制器
              textInputAction: TextInputAction.done,
              controller: controller,
              maxLines: 1,
              textAlign: TextAlign.center,
              //是否自动更正
              autocorrect: true,
              //是否自动对焦
              autofocus: false,
              style: TextStyle(
                  color: inputColor,
                  fontSize: 28.sp,
                  fontWeight: FontWeight.w600
              ),
              inputFormatters: [

              ],
              decoration: InputDecoration(
                contentPadding: const EdgeInsets.symmetric(vertical: 0,horizontal: 0),
                hintText: '',
                hintStyle: TextStyle(
                    color: inputColor,
                    fontSize: 32.sp,
                    fontWeight: FontWeight.w600
                ),
                counterText: "",
                border: OutlineInputBorder(
                    borderSide: BorderSide.none),
              ),
            ),
          ),
          SizedBox(height: 40.w,),
          Container(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                GestureDetector(
                  onTap: ()async{
                    FocusScope.of(context).requestFocus(FocusNode());
                    SmartDialog.dismiss();
                  },
                  child: Container(
                    width: 290.w,
                    height: 92.w,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                        color: Color(0xff666666),
                        borderRadius: BorderRadius.circular(100.w)
                    ),
                    child: Text('Cancel'.tr,style: TextStyle(
                        color: Colors.white,
                        fontSize: 28.sp,
                        fontWeight: FontWeight.w600
                    ),),
                  ),
                ),
                GestureDetector(
                  onTap: ()async{
                    FocusScope.of(context).requestFocus(FocusNode());
                    if(controller.text!=null && controller.text.trim().isNotEmpty){

                      Map<String,dynamic> data = {
                        'sn': baseDeviceNum??'',
                        'value': _changeNum(controller.text),
                        'code': widget.item['code']
                      };
                      ResponseData result = await HomeAPI.postDevParameter(data);
                      if(result.code == 200){
                        if(widget.callBack!=null){
                          widget.callBack(controller.text);
                        }
                      }
                    }
                    SmartDialog.dismiss();
                  },
                  child: Container(
                    width: 290.w,
                    height: 92.w,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                        color: themeColor,
                        borderRadius: BorderRadius.circular(100.w)
                    ),
                    child: Text('Save'.tr,style: TextStyle(
                        color: Colors.white,
                        fontSize: 28.sp,
                        fontWeight: FontWeight.w600
                    ),),
                  ),
                )
              ],
            ),
          )
        ],
      ),
    );
  }

  _changeNum(String val){
    String res = '';
    int num = 4 - val.length;
    for(int i=0;i<num;i++){
      res += '0';
    }
    res += val;
    return res;
  }
}
